//
//  MessageFrame.swift
//  QQ
//
//  Created by zhang on 16/1/7.
//  Copyright © 2016年 jin. All rights reserved.
//

import UIKit

class MessageFrame: NSObject {
    let iconFrame:CGRect
    let timeFrame:CGRect
    let textFrame:CGRect
    let cellHeight:CGFloat
    let message:Message
    //快速实例话的方法
    class func instanceWithFile()->[MessageFrame]
    {
        let path = NSBundle.mainBundle().pathForResource("messages", ofType: "plist")
        let data = NSArray(contentsOfFile: path!)
        var messageFrames:[MessageFrame] = []
        //这个变量用于记录上一个消息时间，用这个判断当前messageCell是否显示时间lable
        var flag:NSString = ""
        for var i = 0;i < data?.count;i++
        {
            let message = Message(dic: data![i] as! [String : AnyObject])
            //判断是否显示时间
            if flag.isEqualToString(message.time as String)
            {
                message.hiddenTime = false
            }
            flag = message.time
            messageFrames.append(MessageFrame.init(message: message))
        }
        return messageFrames
    }
    convenience init(message:Message) {
        let margin:CGFloat = 10
        //计算头像frame
        var iconFrame:CGRect = CGRect.zero
        let iconW:CGFloat = 50
        let iconH:CGFloat = 50
        var iconX:CGFloat = margin
        let iconY:CGFloat = margin
        //当是自己的消息的时候，更改坐标位置
        if message.type == 0
        {
            iconX = UIScreen.mainScreen().bounds.size.width - margin - iconW
        }
        iconFrame = CGRectMake(iconX, iconY, iconW, iconH)
        //时间lable
        var timeFrame:CGRect = CGRect.zero
        if message.hiddenTime
        {
            let timeW = UIScreen.mainScreen().bounds.size.width
            //计算文字所需要的大小
            let timeH = message.time.boundingRectWithSize(CGSizeMake(CGFloat.max, CGFloat.max), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName:UIFont.systemFontOfSize(timeFontSize)], context: nil).size.height
            timeFrame = CGRectMake(0, 0, timeW, timeH)
        }
        //消息Button
        //计算消息文本需要的大小
        let textSize = message.text.boundingRectWithSize(CGSize(width: UIScreen.mainScreen().bounds.size.width - (iconFrame.size.width * 2) - (margin * 4),height: CGFloat.max), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName:UIFont.systemFontOfSize(textFontSize)], context: nil).size
        var textX = CGRectGetMaxX(iconFrame) + margin
        let textY = CGRectGetMaxY(timeFrame) + margin
        //当是自己的消息的时候,更改横坐标
        if message.type == 0
        {
            textX = iconFrame.origin.x - textSize.width - margin - (buttonEdgeInsets * 2)
        }
        //因为设置了Button内边距，所以需要加上内边距
        let textFrame = CGRectMake(textX, textY, textSize.width + (buttonEdgeInsets * 2), textSize.height + 40)
        //cell高度
        let cellHeight = (CGRectGetMaxY(textFrame) > CGRectGetMaxY(iconFrame) ? CGRectGetMaxY(textFrame) : CGRectGetMaxY(iconFrame)) + margin
        //执行构造方法
        self.init(iconFrame:iconFrame,timeFrame:timeFrame,textFrame:textFrame,cellHeight:cellHeight,message:message)
    }
    init(iconFrame:CGRect,timeFrame:CGRect,textFrame:CGRect,cellHeight:CGFloat,message:Message) {
        self.iconFrame = iconFrame
        self.timeFrame = timeFrame
        self.textFrame = textFrame
        self.cellHeight = cellHeight
        self.message = message
    }
}
